package com.jodinfo.outmirror.util;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

public class PicFileUpload {
	//文件上传  
    public static String uploadFile(MultipartFile file, HttpServletRequest request,String id) throws IOException {  
        String fileName = file.getOriginalFilename();  
        String path=request.getSession().getServletContext().getRealPath("images/"+id);
        File pictureFile = new File(path);
        if (pictureFile.isDirectory()){//判断file是否是文件目录 若是返回TRUE  
        	 String name[]=pictureFile.list();//name存储file文件夹中的文件名
        	 for (int i=0; i<name.length; i++){  
                 File f=new File(pictureFile, name[i]);//此时就可得到文件夹中的文件  
                 f.delete();//删除文件  
             }  
        }
        File tempFile = new File(path, new Date().getTime() + String.valueOf(fileName));  
        if (!tempFile.getParentFile().exists()) {  
            tempFile.getParentFile().mkdir();  
        }  
        if (!tempFile.exists()) {  
            tempFile.createNewFile();  
        }  
        file.transferTo(tempFile);  
        return "images/" +id+"/"+tempFile.getName();  
    }  
}
